package com.xkh.common.service.impl;

import com.alibaba.excel.EasyExcel;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.xkh.common.entity.DemoData;
import com.xkh.common.service.IExcelService;
import com.xkh.common.util.PdfFontUtil;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author 许克弘
 * @date 2021/6/1 22:35
 */
@Service
public class ExcelServiceImpl implements IExcelService {

    @Override
    public void export(List<DemoData> list) {
        //指定路径 文件流会自动关闭
        String fileName = "D://excel//" + UUID.randomUUID() + ".xlsx";
        EasyExcel.write(fileName, DemoData.class).sheet("模板").doWrite(list);
    }

    @Override
    public void pdfExport(HttpServletResponse response) {

        Document document = null;
        PdfWriter writer = null;
        try {
            ServletOutputStream out = response.getOutputStream();
            document = new Document();
            writer = PdfWriter.getInstance(document, out);
            document.open();
            getDocument(document);
            document.newPage();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (document != null) {
                document.close();
            }
            if (writer != null) {
                writer.close();
            }
        }
    }

    /**
     * 生成PDF内容
     * @param document
     */
    public void getDocument(Document document) throws Exception {
        //报告标题
        document.add(PdfFontUtil.getParagraph(PdfFontUtil.PAGE_TITLE,PdfFontUtil.TITLE_FONT,1)) ;
        document.add(PdfFontUtil.getParagraph("\n商户名称：XXX科技有限公司",PdfFontUtil.INFO_FONT,-1)) ;
        document.add(PdfFontUtil.getParagraph("\n生成时间：2021-09-02\n\n",PdfFontUtil.INFO_FONT,-1)) ;
        // 报告内容
        // 段落标题 + 报表图
        document.add(PdfFontUtil.getParagraph("城市数据分布统计",PdfFontUtil.NODE_FONT,-1)) ;
        document.add(PdfFontUtil.getParagraph("\n· 可视化图表\n\n",PdfFontUtil.BLOCK_FONT,-1)) ;
        // 设置图片宽高
        float documentWidth = document.getPageSize().getWidth() - document.leftMargin() - document.rightMargin();
        float documentHeight = documentWidth / 580 * 320;
        document.add(PdfFontUtil.getImage("C:\\Users\\Administrator\\Desktop\\7fc195999df7441a655c1109fadfd17.jpg",documentWidth-80,documentHeight-80)) ;
        // 数据表格
        document.add(PdfFontUtil.getParagraph("\n· 数据详情\n\n",PdfFontUtil.BLOCK_FONT,-1)) ;
        PdfPTable dataTable = PdfFontUtil.getPdfPTable01(6,500) ;
        // 设置表格
        List<String> tableHeadList = tableHead () ;
        List<List<String>> tableDataList = getTableData () ;
        PdfFontUtil.addTableCell(dataTable,PdfFontUtil.CONTENT_FONT,tableHeadList);
        for (List<String> tableData : tableDataList) {
            PdfFontUtil.addTableCell(dataTable,PdfFontUtil.CONTENT_FONT,tableData);
        }
        document.add(dataTable);
        document.add(PdfFontUtil.getParagraph("\n· 报表描述\n\n",PdfFontUtil.BLOCK_FONT,-1)) ;
        document.add(PdfFontUtil.getParagraph("数据报告可以监控每天的推广情况，" +
                "可以针对不同的数据表现进行分析，以提升推广效果。",PdfFontUtil.CONTENT_FONT,-1)) ;
    }

    private static List<String> tableHead (){
        List<String> tableHeadList = new ArrayList<>() ;
        tableHeadList.add("省份") ;
        tableHeadList.add("城市") ;
        tableHeadList.add("数量") ;
        tableHeadList.add("百分比") ;
        tableHeadList.add("百分比") ;
        tableHeadList.add("百分比") ;
        return tableHeadList ;
    }

    private static List<List<String>> getTableData (){
        List<List<String>> tableDataList = new ArrayList<>() ;
        for (int i = 0 ; i < 3 ; i++){
            List<String> tableData = new ArrayList<>() ;
            tableData.add("浙江"+i) ;
            tableData.add("杭州"+i) ;
            tableData.add("276"+i) ;
            tableData.add("33.3%") ;
            tableData.add("33.3%") ;
            tableData.add("33.3%") ;
            tableDataList.add(tableData) ;
        }
        return tableDataList ;
    }
}
